# handlers.py

from PyQt5.QtCore import pyqtSignal, QObject
from trace_visualizer_py.common import *

class Handler(QObject):

    sig_uwb_mode_update = pyqtSignal(int)

    def __init__(self):
        super().__init__()

    def spinBox_PoolLength_ValueChanged(self, ui, cfg):
        cfg.set('poolLength', ui.spinBox_PoolLength.value())
        common_data['widget_trace'].resize_pool(ui.spinBox_PoolLength.value(), ui.spinBox_PoolWidth.value())

    def spinBox_PoolWidth_ValueChanged(self, ui, cfg):
        cfg.set('poolWidth', ui.spinBox_PoolWidth.value())
        common_data['widget_trace'].resize_pool(ui.spinBox_PoolLength.value(), ui.spinBox_PoolWidth.value())

    def spinBox_UWB1_X_ValueChanged(self, ui, cfg):
        cfg.set('UWB1_pos', [ui.spinBox_UWB1_X.value(), cfg.get('UWB1_pos')[1]])
    
    def spinBox_UWB1_Y_ValueChanged(self, ui, cfg):
        cfg.set('UWB1_pos', [cfg.get('UWB1_pos')[0], ui.spinBox_UWB1_Y.value()])
    
    def spinBox_UWB2_X_ValueChanged(self, ui, cfg):
        cfg.set('UWB2_pos', [ui.spinBox_UWB2_X.value(), cfg.get('UWB2_pos')[1]])
    
    def spinBox_UWB2_Y_ValueChanged(self, ui, cfg):
        cfg.set('UWB2_pos', [cfg.get('UWB2_pos')[0], ui.spinBox_UWB2_Y.value()])

    def spinBox_UWB3_X_ValueChanged(self, ui, cfg):
        cfg.set('UWB3_pos', [ui.spinBox_UWB3_X.value(), cfg.get('UWB3_pos')[1]])
    
    def spinBox_UWB3_Y_ValueChanged(self, ui, cfg):
        cfg.set('UWB3_pos', [cfg.get('UWB3_pos')[0], ui.spinBox_UWB3_Y.value()])

    def spinBox_PID_P_ValueChanged(self, ui, cfg):
        cfg.set('PID1', [ui.spinBox_PID_P.value(), cfg.get('PID1')[1], cfg.get('PID1')[2]])

    def spinBox_PID_I_ValueChanged(self, ui, cfg):
        cfg.set('PID1', [cfg.get('PID1')[0], ui.spinBox_PID_I.value(), cfg.get('PID1')[2]])
    
    def spinBox_PID_D_ValueChanged(self, ui, cfg):
        cfg.set('PID1', [cfg.get('PID1')[0], cfg.get('PID1')[1], ui.spinBox_PID_D.value()])

    def spinBox_PID_P_2_ValueChanged(self, ui, cfg):
        cfg.set('PID2', [ui.spinBox_PID_P_2.value(), cfg.get('PID2')[1], cfg.get('PID2')[2]])

    def spinBox_PID_I_2_ValueChanged(self, ui, cfg):
        cfg.set('PID2', [cfg.get('PID2')[0], ui.spinBox_PID_I_2.value(), cfg.get('PID2')[2]])
    
    def spinBox_PID_D_2_ValueChanged(self, ui, cfg):
        cfg.set('PID2', [cfg.get('PID2')[0], cfg.get('PID2')[1], ui.spinBox_PID_D_2.value()])

    def pushButton_updateUWB_click(self, ui, cfg):
        x1, y1 = ui.spinBox_UWB1_X.value(), ui.spinBox_UWB1_Y.value()
        x2, y2 = ui.spinBox_UWB2_X.value(), ui.spinBox_UWB2_Y.value()
        x3, y3 = ui.spinBox_UWB3_X.value(), ui.spinBox_UWB3_Y.value()

        cfg.set('UWB1_pos', [x1, y1])
        cfg.set('UWB2_pos', [x2, y2])
        cfg.set('UWB3_pos', [x3, y3])

        common_data['widget_trace'].update_uwb_location(x1, y1, x2, y2, x3, y3)

        cfg.save_to_file()
        
        print('已更新基站配置')

    def pushButton_uwbModeSet_click(self, ui, cfg):
        # ui.pushButton_uwbModeSet.setEnabled(False)
        # ui.pushButton_uwbModeUnSet.setEnabled(True)
        self.sig_uwb_mode_update.emit(1)
    
    def pushButton_uwbModeUnSet_click(self, ui, cfg):
        # ui.pushButton_uwbModeSet.setEnabled(True)
        # ui.pushButton_uwbModeUnSet.setEnabled(False)
        self.sig_uwb_mode_update.emit(2)

    def pushButton_StartShip_click(self, ui, cfg):
        cfg.set('shipState', ShipState.START)


    def pushButton_StopShip_click(self, ui, cfg):
        cfg.set('shipState', ShipState.STOP)
    

common_data['handler_ui'] = Handler()

    
